package top.jacktgq.candychat.netty;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author CandyWall
 * @Date 2021/2/23--14:43
 * @Description 记录用户ID和Channel之间的对应关系的map
 */
public class UserIdChannelMap {
    private ConcurrentHashMap<String, Channel> map = new ConcurrentHashMap<>();

    public void put(String userId, Channel channel) {
        map.put(userId, channel);
    }

    public Channel get(String userId) {
        return map.get(userId);
    }

    public void removeByChannel(Channel channel) {
        String userId = null;
        for (Map.Entry<String, Channel> entry : map.entrySet()) {
            if (entry.getValue().equals(channel)) {
                userId = entry.getKey();
                break;
            }
        }
        map.remove(userId);
        System.out.println("从userIdChannelMap中移除了userId为 " + userId + " 的记录");
    }
}
